package com.lagou.homework.test;

import com.lagou.homework.entity.Employee;
import com.lagou.homework.utils.JDBCUtils;

import java.sql.*;

/*
 * @className JDBC_PrepareStatement
 * @description JDBC测试类
 * @author admin
 * @date 2022/4/11 15:21
 * */
public class JDBC_PrepareStatement {

    public static void main(String[] args) {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            // 1.获取连接
            con = JDBCUtils.getConnection();
            // 2.编写sql
            String sql = "select * from employee where name = ? && gender = ?";
            // 3.获取预处理对象
            ps = con.prepareStatement(sql);
            // 4.设置占位符参数
            ps.setString(1,"林黛玉");
            ps.setString(2,"女");
            // 5.执行sql 并获取结果集
            rs = ps.executeQuery();
            // 6.遍历结果集并将获取的数据存放在Employee对象中并打印
            while(rs.next()){
                int id = rs.getInt("id");
                String name = rs.getString("name");
                String gender = rs.getString("gender");
                double salary = rs.getDouble("salary");
                double bonus = rs.getDouble("bonus");
                Date join_date = rs.getDate("join_date");
                Employee employee = new Employee(id, name, gender, salary, bonus, join_date);
                System.out.println(employee);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            // 7.释放资源
            JDBCUtils.close(con,ps,rs);
        }
    }
}
